

#include "tcp_server.h"
#include "../../common/log.h"
#include "../eventloop.h"
#include "tcp_connection.h"

namespace rocket {

TcpServer::TcpServer(NetAddr::s_ptr local_addr) : m_local_addr(local_addr) {

    init();

    INFOLOG("rocket TcpServer Listen success on [%s]",
            m_local_addr->toString().c_str());
}

TcpServer::~TcpServer() {
    if (m_main_event_loop) {
        delete m_main_event_loop;
        m_main_event_loop = nullptr;
    }
}

void TcpServer::init() {

    m_acceptor = std::make_shared<TcpAcceptor>(m_local_addr);

    m_main_event_loop = EventLoop::GetCurrentEventLoop();
    m_io_thread_group = new IOThreadGroup(2);
    m_listen_fd_event = new FdEvent(m_acceptor->getListenFd());
    m_listen_fd_event->listen(FdEvent::IN_EVENT,
                              std::bind(&TcpServer::onAccept, this));

    m_main_event_loop->addEpollEvent(m_listen_fd_event);
}

void TcpServer::onAccept() {
    auto re = m_acceptor->accept();
    int client_fd = re.first;
    NetAddr::s_ptr peer_addr = re.second;

    IOThread *io_thread = m_io_thread_group->getIOThread();

    TcpConnection::s_ptr connection = std::make_shared<TcpConnection>(
        io_thread->getEventLoop(), client_fd, 128, m_local_addr, peer_addr);
    connection->setState(TcpConnection::Connected);
    connection->setConnectionType(TcpConnection::TcpConnectionByServer);
    m_client.insert(connection);

    m_client_counts++;

    INFOLOG("TcpServer succ get client, fd=%d", client_fd);
}
void TcpServer::start() {
    m_io_thread_group->start();

    // will stuck here.
    m_main_event_loop->loop();
}

} // namespace rocket
